package com.rongxunwu.netty.learning;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * TODO
 *
 * @author WuRongXun
 * @version 1.0
 * @date 2021/11/1 22:38
 */
public class NioClient {
    public static void main(String[] args) throws IOException {
        //得到一个网络通道
        SocketChannel socketChannel=SocketChannel.open();
        //设置非阻塞
        socketChannel.configureBlocking(false);
        //设置提供服务的服务器ip和端口
        InetSocketAddress inetSocketAddress=new InetSocketAddress("127.0.0.1",8080);
        //连接服务器
        if (!socketChannel.connect(inetSocketAddress)){
            while (!socketChannel.finishConnect()){
                System.out.printf("因为连接需要时间，客户端不阻塞，可以做其他工作；");
            }
        }
        String string="hello world";
        ByteBuffer byteBuffer=ByteBuffer.wrap(string.getBytes());
        //发送数据 将buffer 数据写入 channel
        socketChannel.write(byteBuffer);
        System.in.read();
    }
}
